package com.apes.fn.business.insurance.modle;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.plugin.soe.metadata.BusinessAttributeValue;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.*;

/**
 * @program: apes-server
 * @description 车险有效保单修改
 * @author: czs
 * @create: 2022-02-10 11:44
 **/
@Setter
@Getter
@Entity
@SelectBeforeUpdate
@EntityListeners(DefaultPersistListener.class)
@Table(name = InsurancePolicyFix.CONST_TABLE_NAME)
@DynamicInsert
@DynamicUpdate
public class InsurancePolicyFix  extends Model {
    public static final String CONST_TABLE_NAME = "insurance_Policy_fix";

    @Id
    @Column(name = "id", nullable = false)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "修改单号",prefix = "1")
    @Comment("修改单号")
    private String id;

    @States(name = "佣金类型", values = {
            @State(id = "A", name = "正常佣金"),
            @State(id = "B", name = "追加佣金"),
            @State(id = "C", name = "退保佣金"),
            @State(id = "D", name = "退保追加佣金")
    })
    @Comment("佣金类型")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "commission_type", nullable = false, referencedColumnName = "id")
    private BusinessAttributeValue commissionType;

    @Name
    @Column(name = "remark", length = 120)
    @Comment("备注")
    private String remark;

    @States(name = "单据状态", values = {
            @State(id = "create", name = "新增"),
            @State(id = "delete", name = "删除"),
            @State(id = "save", name = "审核")
    })
    @Column(name = "state", length = 32, nullable = false)
    @Comment("单据状态")
    private String state = "create";

    @Comment("删除人")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_id", referencedColumnName = "id")
    private PartyRole deleteId;

    @Comment("保存人")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "save_id", referencedColumnName = "id")
    private PartyRole saveId;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "approve_Date")
    @Comment("审核时间")
    private Date approveDate;

    @OneToMany(mappedBy = "insurancePolicyFix",cascade = CascadeType.ALL,orphanRemoval = true)
    private Set<InsurancePolicyFixItem> insurancePolicyFixItems = new HashSet<>();

    public Set<InsurancePolicyFixItem> getInsurancePolicyFixItems() {
        for (InsurancePolicyFixItem item : insurancePolicyFixItems) {
            item.setInsurancePolicyFix(this);
        }
        return insurancePolicyFixItems;
    }

}
